home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10303 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: telepost.no!usenet
  2. From: Carsten Arnholm <ca@sesam.dnv.no>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: question assignment operator in c++
  5. Date: 7 Mar 1996 13:17:03 GMT
  6. Organization: DNV
  7. Message-ID: <4hmnkf$t1s@nms.telepost.no>
  8. References: <menghua_wang.1.002DB7BE@muccmail.missouri.edu> <4hl7i7$81a@clarknet.clark.net>
  9. NNTP-Posting-Host: hugin.sesam.dnv.no
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1 (Windows; I; 32bit)
  14.  
  15. gusty@clark.net (Harlan Messinger) wrote:
  16. >menghua wang (menghua_wang@muccmail.missouri.edu) wrote:
  17. >: I do not why the assignment operator can only be overloaded as a member 
  18. >: function in a class such as:
  19. >: 
  20. >:           class myclass {
  21. >:               int i;
  22. >:           public:
  23. >:               myclass operator= (myclass &A) {
  24. >:                  i = A.i;
  25. >:                  return *this;
  26. >:               }
  27. >:           };
  28. >: 
  29. >: Why does not the following frind overloading work?
  30. >: 
  31. >:               mycalss operator= (myclass &A, myclass &B) {
  32. >:                    A.i = B.i;
  33. >:                    return A;
  34. >:               }
  35. >
  36. >I believe it's because each class has a default assignment operator as an
  37. >implicit member function. Therefore, if you want to write your own, you
  38. >have to overload the existing implied one, which calls for an explicit
  39. >member function. That, or perhaps there would be inheritance problems. 
  40. >
  41.  
  42.  
  43. I'm more inclined to believe the reason is that you are not supposed to
  44. redefine behaviour of assignment to basic types such as int, float .. etc.
  45. which would occur if you were allowed to overload the global assignment.
  46.  
  47. Carsten Arnholm
  48.  
  49.